fn main() {
    let strs = vec![
        "flower".to_string(),
        "flow".to_string(),
        "flight".to_string(),
    ];
    let ans = Solution::longest_common_prefix(strs);
    println!("{}", ans);
}

struct Solution;

impl Solution {
    pub fn longest_common_prefix(strs: Vec<String>) -> String {
        let mut ans = String::new();
        let mut list: Vec<std::str::Chars> = strs.iter().map(|x| x.chars()).collect();
        let count = strs.len();
        let mut set = std::collections::HashSet::new();
        let mut flag = false;
        loop {
            for i in 0..count {
                if let Some(c) = list[i].next() {
                    set.insert(c);
                    if i == count - 1 {
                        if set.len() == 1 {
                            ans.push(c);
                            set.clear();
                        } else {
                            flag = true;
                            break;
                        }
                    }
                } else {
                    flag = true;
                    break;
                }
            }
            if flag {
                break;
            }
        }
        ans
    }
}
